home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 15490 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.3 KB

  1. From: jamie@pro-net.co.uk (James Dickson)
  2. Newsgroups: comp.lang.c
  3. Subject: Re: File Display Program
  4. Date: Fri, 19 Apr 1996 10:01:25 GMT
  5. Message-ID: <317762d2.1345179@news.globalnet.co.uk>
  6. References: <4l6nrd$fr3@freenet-news.carleton.ca>
  7. X-Newsreader: Forte Agent .99d/32.182
  8. NNTP-Posting-Host: client8114.globalnet.co.uk
  9. Path: news.globalnet.co.uk!
  10.  
  11. On 19 Apr 1996 00:47:41 GMT, aq436@FreeNet.Carleton.CA (Jerry Boyd)
  12. wrote:
  13.  
  14. >
  15. >How would I write a program that would display the contents of 
  16. >a file 20 lines at a time.  At the end of each 20 lines, I need
  17. >it to wait for a character to be entered (in the form of "q" to 
  18. >quit the display and any other character to continue with the 
  19. >next 20 lines of the file)?
  20. >
  21. >
  22. >
  23. >--
  24.  
  25.  
  26. Try something like:
  27.  
  28. #include <stdio.h>
  29.  
  30. main()
  31. {
  32.  
  33. FILE    *fpInput    =    fopen( "myfile","r" );
  34. int    iCounter    =    0;
  35. char    szRecord[ 1024 ];
  36.  
  37. if ( fpInput )
  38.   {
  39.     while( fgets( szRecord, sizeof( szRecord ), fpInput ) )
  40.       {
  41.         printf( "%s", szRecord );
  42.         if ( (++iCounter) == 20 )
  43.           {
  44.             printf( "\nEnter Command:" );
  45.             if ( getchar() == 'q' )
  46.               exit(0);
  47.             iCounter = 0;            
  48.           }
  49.       }
  50.   }
  51. else
  52.   perror( "fopen" );
  53.  
  54. }
  55.  
  56.  
  57. I haven't compiled this but it should give you some good pointers.
  58.  
  59.  
  60. James Dickson.
  61.